package git.soulbgm.controller;

import git.soulbgm.common.enums.Status;
import git.soulbgm.common.pojo.PageInfo;
import git.soulbgm.common.pojo.ResultData;
import git.soulbgm.common.pojo.ReturnCode;
import git.soulbgm.pojo.dto.MenuDto;
import git.soulbgm.pojo.entity.Menu;
import git.soulbgm.pojo.vo.MenuTreeVo;
import git.soulbgm.security.pojo.LoginUser;
import git.soulbgm.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 角色管理接口
 *
 * @author SoulBGM
 * @date 2020-09-10 17:14:11
 */
@RequestMapping("menu")
@RestController
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("findById")
    public ResultData findById(Long id) {
        Menu menu = menuService.findById(id);
        return ResultData.getResultData(ReturnCode.SUCCESS, menu);
    }

    @GetMapping("find")
    public ResultData find(MenuDto menu) {
        PageInfo pageInfo = menuService.find(menu.getPageNum(), menu.getPageSize(), menu);
        return ResultData.getResultData(ReturnCode.SUCCESS, pageInfo);
    }

    @GetMapping("findTree")
    public ResultData findTree() {
        List<MenuTreeVo> list = menuService.findTree();
        return ResultData.getResultData(ReturnCode.SUCCESS, list);
    }

    @GetMapping("roleMenuTreeSelect")
    public ResultData roleMenuTreeSelect(Long roleId) {
        List<Long> menuIdList = menuService.getMenuIdList(roleId);
        List<MenuTreeVo> allMenuTree = menuService.findTree();
        Map<String, List<?>> map = new HashMap<>(2);
        map.put("checkedKeys", menuIdList);
        map.put("menus", allMenuTree);
        return ResultData.getResultData(ReturnCode.SUCCESS, map);
    }

    @GetMapping("findByRoleId")
    public ResultData findByRoleId(Long roleId) {
        List<MenuTreeVo> treeVoList = menuService.findByRoleId(roleId);
        return ResultData.getResultData(ReturnCode.SUCCESS, treeVoList);
    }

    @GetMapping("findLoginMenu")
    public ResultData findByUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser userDetails = (LoginUser) authentication.getPrincipal();
        List<MenuTreeVo> treeVoList = menuService.findByUserId(userDetails.getId());
        return ResultData.getResultData(ReturnCode.SUCCESS, treeVoList);
    }

    @PostMapping("add")
    public ResultData registered(@RequestBody MenuDto menuDto) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuDto, menu);
        menu.setDelFlag(Status.EXIST.code);
        menu.setStatus(Status.NORMAL.code);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String createUser = "admin";
        if (authentication != null) {
            LoginUser userDetails = (LoginUser) authentication.getPrincipal();
            createUser = userDetails.getUsername();
        }
        menu.setCreateUser(createUser);
        menu.setUpdateUser(createUser);
        Menu dbMenu = menuService.add(menu);
        if (dbMenu != null) {
            return ResultData.getResultData(ReturnCode.SUCCESS, menu);
        } else {
            return ResultData.getResultData(ReturnCode.SAVE_FAIL);
        }
    }

    @PostMapping("update")
    public ResultData update(@RequestBody MenuDto menuDto) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuDto, menu);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String updateUser = "admin";
        if (authentication != null) {
            LoginUser userDetails = (LoginUser) authentication.getPrincipal();
            updateUser = userDetails.getUsername();
        }
        menu.setUpdateUser(updateUser);
        menu.setUpdateTime(new Date());

        Boolean result = menuService.update(menu);
        if (result) {
            return ResultData.getResultData(ReturnCode.SUCCESS);
        } else {
            return ResultData.getResultData(ReturnCode.UPDATE_FAIL);
        }
    }

    @PostMapping("delete")
    public ResultData delete(String ids) {
        String[] split = ids.split(",");
        List<String> idList = Arrays.asList(split);
        Boolean result = menuService.delete(idList);
        if (result) {
            return ResultData.getResultData(ReturnCode.SUCCESS);
        } else {
            return ResultData.getResultData(ReturnCode.REMOVE_FAIL);
        }
    }

}
